var game = new Phaser.Game(1024, 615, Phaser.CANVAS, "game");

game.GameStates = {};

game.GameStates.loadGame = {
	preload: function() {
		game.load.image("background", "assets/background/bg.jpg");
		game.load.image("btn-background", "assets/ui/btn-bg.png");
	},

	create: function() {
		game.add.sprite(0, 0, "background");

		var gameTitleStyle = {
			font: "65px 宋体",
			fill: "skyblue",
			align: "center"
		};

		var btnTextStyle = {
			font: "32px Arial",
			fill: "white",
			align: "center"
		};

    	var gameTitle = game.add.text(game.world.centerX, game.world.centerY - 150, "泰德拉英雄", gameTitleStyle);
    	gameTitle.anchor.set(0.5);

    	var btnPlay = game.add.button(game.world.centerX, game.world.centerY + 20, "btn-background", function() {
    		game.state.start('chooseHero');
    	});

    	btnPlay.anchor.set(0.5, 0.5);
    	btnPlay.width = 160;

    	var btnText = game.add.text(game.world.centerX, game.world.centerY + 20, "开始游戏", btnTextStyle);
    	btnText.anchor.set(0.5);
	}
};

var Edgard = {
	name: "埃德拉",
	key: "edgard",
	sex: "男",
	job: "武士|法师",
	hp: 200,
	pp: 150,
	atk: 10,
	describe: "勇敢的力量！！！"
};


var Catherine = {
	name: "凯瑟琳",
	key: "catherine",
	sex: "女",
	job: "骑士|法师",
	hp: 150,
	pp: 200,
	atk: 8,
	describe: "美丽的危险！！！"
};

var mainPlayerObject;

var item1;
var item2;

game.GameStates.chooseHero = {
	preload: function() {
		game.load.image("item", "assets/ui/item.png");
		game.load.spritesheet("catherine", "assets/player/catherine.png", 32, 48);
		game.load.spritesheet("edgard", "assets/player/edgard.png", 32, 48);
	},

	create: function() {
		var titleStyle = {
			font: "65px 宋体",
			fill: "skyblue",
			align: "center"
		};

		var title = game.add.text(game.world.centerX, 70, "请选择英雄", titleStyle);
    	title.anchor.set(0.5);

		item1 = new Item(0, 0);
		item1.item.centerX = 60 + item1.item.width / 2;
		item1.item.centerY = 150 + item1.item.height / 2;
		item1.appendPlayerObject(Edgard);
		item1.appendButton();

		item2 = new Item(0, 0);
		item2.item.centerX = 60 + item1.item.centerX + item1.item.width;
		item2.item.centerY = 150 + item2.item.height / 2;
		item2.appendPlayerObject(Catherine);
		item2.appendButton();

		var item3 = new Item(0, 0);
		item3.item.centerX = 60 + item2.item.centerX + item2.item.width;
		item3.item.centerY = 150 + item3.item.height / 2;
		//item3.appendButton();

		var item4 = new Item(0, 0);
		item4.item.centerX = 60 + item3.item.centerX + item3.item.width;
		item4.item.centerY = 150 + item4.item.height / 2;
		//item4.appendButton();
	}
};

function Item(x, y, key) {
	this.item = game.add.button(0, 0, "item");

	var _this = this;

	this.item.onInputDown.add(function() {
		_this.player.kill();

    	var nameTextStyle = {
			font: "37px Arial",
			fill: "skyblue",
			align: "center"
		};

		var informationTextStyle = {
			font: "17px Arial",
			fill: "white",
		};


    	_this.nameText = game.add.text(_this.item.centerX, _this.item.y + 25, _this.playerObject.name, nameTextStyle);
		_this.nameText.anchor.set(0.5, 0.5);

		_this.informationText = game.add.text(_this.item.x + 10, _this.item.y + 70, "性别：" + _this.playerObject.sex + "\n职业：" + _this.playerObject.job + "\n血量：" + _this.playerObject.hp + "\n魔法值：" + _this.playerObject.pp + "\n攻击力：" + _this.playerObject.atk + "\n格言：" + _this.playerObject.describe, informationTextStyle);

	});

	this.item.onInputUp.add(function() {
		_this.appendPlayerObject(_this.playerObject);
		_this.nameText.kill();
		_this.informationText.kill();
	});

	console.log(this.item);
}

Item.prototype.appendPlayerObject = function(playerObject) {
	this.playerObject = playerObject;
	this.player = game.add.sprite(this.item.centerX, this.item.centerY, playerObject.key);
	this.player.scale.setTo(3, 3);
	this.player.anchor.set(0.5, 0.5);
	this.player.animations.add("work", [0, 1, 2, 3], 7, true);
	this.player.animations.play("work", true);
};

Item.prototype.appendButton = function() {
	var _this = this;

	var btnPlay = game.add.button(this.item.centerX, this.item.centerY + 170, "btn-background", function() {
		mainPlayerObject = _this.playerObject;

		game.state.start("main");
	});

    btnPlay.anchor.set(0.5, 0.5);
    btnPlay.width = 160;

    var btnTextStyle = {
		font: "32px Arial",
		fill: "white",
		align: "center"
	};

    var btnText = game.add.text(this.item.centerX, this.item.centerY + 170, "选定", btnTextStyle);
    btnText.anchor.set(0.5);
};

var mainPlayer;
var ice;

var btn_left;
var btn_right;
var btn_up;

var NO_DIRECTION = 0;
var LEFT = 1;
var RIGHT = 2;

game.GameStates.main = {
	preload: function() {
		game.load.image("main_bg", "assets/background/main_bg.png");
		game.load.image("ice", "assets/background/ice.png");
		game.load.image("btn-move", "assets/ui/btn-move.png");
		game.load.image("btn-up", "assets/ui/btn-up.png");
	},

	create: function() {
		game.physics.startSystem(Phaser.Physics.ARCADE);

		var main_bg = game.add.sprite(0, 0, "main_bg");
		main_bg.width = game.world.width;
		main_bg.height = game.world.height;

		ice = game.add.tileSprite(0, 0, 0, 0, "ice");
		ice.width = game.world.width;
		ice.height = ice.height / 2;
		ice.y = game.world.height - ice.height;

		game.physics.arcade.enable(ice);

		ice.body.immovable = true;

		mainPlayer = new MainPlayer(mainPlayerObject);
		mainPlayer.player.body.collideWorldBounds = true;
		mainPlayer.player.animations.add("left", mainPlayer.player_left, 8);
		mainPlayer.player.animations.add("right", mainPlayer.player_right, 8);
		
		btn_left = game.add.button(0, 0, "btn-move");
		btn_left.x = 60 + btn_left.width;
		btn_left.y = game.world.height - btn_left.height - 70;
		btn_left.scale.set(-1, 1);
		
		btn_right = game.add.button(0, 0, "btn-move");
		btn_right.x = 60 + btn_right.width + btn_left.x + btn_left.width;
		btn_right.y = btn_left.y;
		
		btn_up = game.add.button(0, 0, "btn-up");
		btn_up.x = game.world.width - btn_up.width - 240;
		btn_up.y = btn_left.y;
		
		//btn_left.onInputOver.add(function() {mainPlayer.goToLeft = true;});
		btn_left.onInputDown.add(function() {mainPlayer.goToLeft = true;});
		//btn_left.onInputOut.add(function() {mainPlayer.goToLeft = false;});
		btn_left.onInputUp.add(function() {mainPlayer.goToLeft = false;});
		
		//btn_right.onInputOver.add(function() {mainPlayer.goToRight = true;});
		btn_right.onInputDown.add(function() {mainPlayer.goToRight = true;});
		//btn_right.onInputOut.add(function() {mainPlayer.goToRight = false;});
		btn_right.onInputUp.add(function() {mainPlayer.goToRight = false;});
		
		//btn_up.onInputOver.add(function() {mainPlayer.jump = true;});
		btn_up.onInputDown.add(function() {mainPlayer.jump = true;});
		//btn_up.onInputOut.add(function() {mainPlayer.jump = false;});
		btn_up.onInputUp.add(function() {mainPlayer.jump = false;});
	},

	update: function() {
		game.physics.arcade.collide(mainPlayer.player, ice);
		
		if(mainPlayer.goToLeft) {
			mainPlayer.player.body.velocity.x = -150;
			mainPlayer.player.animations.play("left");
			mainPlayer.direction = LEFT;
		} else if(mainPlayer.goToRight) {
			mainPlayer.player.body.velocity.x = 150;
			mainPlayer.player.animations.play("right");
			mainPlayer.direction = RIGHT;
		} else if(mainPlayer.jump && mainPlayer.player.body.touching.down) {
			mainPlayer.player.body.velocity.y = -250;
		} else {
			mainPlayer.player.body.velocity.x = 0;
			mainPlayer.player.animations.stop();
		}
	}
};

function MainPlayer(mainPlayerObject) {
	this.player = game.add.sprite(0, 0, mainPlayerObject.key);
	this.player.centerX = game.world.centerX;
	this.player.centerY = game.world.centerY + 30;
	this.player.scale.setTo(2, 2);
	this.player_left = [4, 5, 6, 7];
	this.player_right = [8, 9, 10, 11];
	
	this.goToLeft = false;
	this.goToRight = false;
	this.jump = false;
	
	this.direction = NO_DIRECTION;

	game.physics.arcade.enable(this.player);

	this.player.body.gravity.y = 300;
}

game.state.add("loadGame", game.GameStates.loadGame);
game.state.add("chooseHero", game.GameStates.chooseHero);
game.state.add("main", game.GameStates.main);

game.state.start("loadGame");
